package com.gm.commonlib.network.basenetwork;

import android.text.TextUtils;

import com.gm.commonlib.network.HttpClient;
import com.gm.commonlib.network.basenetwork.gson.IgnoreStrategy;
import com.gm.commonlib.network.basenetwork.interceptor.HttpLoggingInterceptor;
import com.gm.commonlib.network.basenetwork.numberformat.BooleanDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.DateDeserializer;
import com.gm.commonlib.network.basenetwork.numberformat.DateSerializer;
import com.gm.commonlib.network.basenetwork.numberformat.DoubleDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.FloatDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.IntegerDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.LongDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalDouble;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalDoubleDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalInteger;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalIntegerDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalLong;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalLongDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.StringDefault0Adapter;
import com.gm.commonlib.util.logUtils.LogUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.TypeAdapters;
import com.youth.banner.util.LogUtils;

import java.text.DateFormat;
import java.util.Date;

import okhttp3.MediaType;
import okhttp3.RequestBody;
public class RetrofitHelper extends HttpClient {
    private static Gson mGsonInstance;

    public static Gson getFormatedGson() {
        final GsonBuilder builder = new GsonBuilder();
        builder.disableHtmlEscaping();
        builder.addSerializationExclusionStrategy(new IgnoreStrategy());
        builder.registerTypeAdapter(Date.class, new DateDeserializer()).setDateFormat(DateFormat.LONG);
        builder.registerTypeAdapter(Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG);
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(String.class, new StringDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(OriginalInteger.class, new OriginalIntegerDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(OriginalDouble.class, new OriginalDoubleDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(OriginalLong.class, new OriginalLongDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(long.class, Long.class, new LongDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(int.class, Integer.class, new IntegerDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(double.class, Double.class, new DoubleDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(float.class, Float.class, new FloatDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(boolean.class, Boolean.class, new BooleanDefault0Adapter()));
        return builder.create();
    }

    public static Gson getGsonInstance() {
        if (mGsonInstance == null) {
            mGsonInstance = getFormatedGson();
        }
        return mGsonInstance;
    }

    public static <T> T fromJson(String json, Class<T> tClass) {
        try {
            return getGsonInstance().fromJson(json, tClass);
        } catch (Throwable throwable) {
            return null;
        }
    }

    public static String toJson(Object object) {
        try {
            return getGsonInstance().toJson(object);
        } catch (Throwable throwable) {
            return null;
        }
    }

    public static RequestBody getJsonBody(String json) {
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
    }

    /**
     * 获取打印日志拦截器
     *
     * @return HttpLoggingInterceptor
     */
    public static HttpLoggingInterceptor getHttpLoggingInterceptor() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (!TextUtils.isEmpty(message)) {
                    LogUtil.printBigLog(message);
                }
            }
        });
        httpLoggingInterceptor.setLogRequetBody(true);
        httpLoggingInterceptor.setLogResponseBody(true);
        httpLoggingInterceptor.setLogRequetHeader(true);
        return httpLoggingInterceptor;
    }


}
